package com.pm.business.service.impl;

import java.util.List;

import com.pm.common.core.domain.entity.SysUser;
import com.pm.common.core.domain.model.LoginUser;
import com.pm.common.utils.DateUtils;
import com.pm.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.pm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pm.business.domain.BidProjectPresonMap;
import com.pm.business.mapper.BidProjectMapper;
import com.pm.business.domain.BidProject;
import com.pm.business.service.IBidProjectService;

/**
 * 基本信息Service业务层处理
 * 
 * @author zxl
 * @date 2022-05-05
 */
@Service
public class BidProjectServiceImpl implements IBidProjectService 
{
    @Autowired
    private BidProjectMapper bidProjectMapper;

    /**
     * 查询基本信息
     * 
     * @param projectId 基本信息主键
     * @return 基本信息
     */
    @Override
    public BidProject selectBidProjectByProjectId(Long projectId)
    {
        return bidProjectMapper.selectBidProjectByProjectId(projectId);
    }

    /**
     * 查询基本信息列表
     * 
     * @param bidProject 基本信息
     * @return 基本信息
     */
    @Override
    public List<BidProject> selectBidProjectList(BidProject bidProject)
    {
        return bidProjectMapper.selectBidProjectList(bidProject);
    }

    /**
     * 查询基本信息列表
     *
     * @param bidProject 基本信息
     * @return 基本信息
     */
    @Override
    public List<BidProject> selectBidProjectAppList(BidProject bidProject)
    {
        return bidProjectMapper.selectBidProjectAppList(bidProject);
    }

    /**
     * 新增基本信息
     * 
     * @param bidProject 基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBidProject(BidProject bidProject)
    {
        bidProject.setCreateTime(DateUtils.getNowDate());
        bidProject.setCreator(SecurityUtils.getLoginUser().getUsername());
        int rows = bidProjectMapper.insertBidProject(bidProject);
        insertBidProjectPresonMap(bidProject);
        return rows;
    }

    /**
     * 修改基本信息
     * 
     * @param bidProject 基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBidProject(BidProject bidProject)
    {
        bidProject.setUpdateTime(DateUtils.getNowDate());
        bidProjectMapper.deleteBidProjectPresonMapByProjectId(bidProject.getProjectId());
        insertBidProjectPresonMap(bidProject);
        return bidProjectMapper.updateBidProject(bidProject);
    }

    /**
     * 批量删除基本信息
     * 
     * @param projectIds 需要删除的基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBidProjectByProjectIds(Long[] projectIds)
    {
        bidProjectMapper.deleteBidProjectPresonMapByProjectIds(projectIds);
        return bidProjectMapper.deleteBidProjectByProjectIds(projectIds);
    }

    /**
     * 删除基本信息信息
     * 
     * @param projectId 基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBidProjectByProjectId(Long projectId)
    {
        bidProjectMapper.deleteBidProjectPresonMapByProjectId(projectId);
        return bidProjectMapper.deleteBidProjectByProjectId(projectId);
    }

    /**
     * 新增项目与人员关联信息
     * 
     * @param bidProject 基本信息对象
     */
    public void insertBidProjectPresonMap(BidProject bidProject)
    {
        List<BidProjectPresonMap> bidProjectPresonMapList = bidProject.getBidProjectPresonMapList();
        Long projectId = bidProject.getProjectId();
        if (StringUtils.isNotNull(bidProjectPresonMapList))
        {
            List<BidProjectPresonMap> list = new ArrayList<BidProjectPresonMap>();
            for (BidProjectPresonMap bidProjectPresonMap : bidProjectPresonMapList)
            {
                bidProjectPresonMap.setProjectId(projectId);
                list.add(bidProjectPresonMap);
            }
            if (list.size() > 0)
            {
                bidProjectMapper.batchBidProjectPresonMap(list);
            }
        }
    }
}
